﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using ePlanet.SingleWell.Services;
using DreamBuilder.Core.Container;
using ePlanet.SingleWell.Models;
using ePlanet.ArchiveMange.Enum;
using CommonModule;
using ePlanet.ArchiveMange.Services;
using Microsoft.Practices.Unity;
using System.Data;

namespace Web
{
    public class ArchiveBusiHelper
    {
        private ArchiveBusiHelper()
        {

        }

        public static readonly ArchiveBusiHelper Instance = new ArchiveBusiHelper();

        /// <summary>
        /// 备注：根据档案分类和档案ID取电子文件数目
        /// 时间：2013年5月23日 13:24:17
        /// </summary>
        /// <param name="archiveTypeId"></param>
        /// <param name="archiveId"></param>
        /// <returns></returns>
        public string GetTechnologyFileCount(int archiveTypeId, string archiveId)
        {
            string techFileCount = string.Empty;
            IAttachmentService oIAttachmentService = UContainer.Resolve<IAttachmentService>();
            IList<AttachmentModel> attachList = new List<AttachmentModel>();
            List<AttachmentModel> pagefileAttachList = new List<AttachmentModel>();
            List<string> attachIdList = new List<string>();
            DataTable table = new DataTable();
            IPageFileService PageFileService = UContainer.Container.Resolve<IPageFileService>();
            switch (archiveTypeId)
            {
                case (int)ArchiveBaseType.Account:
                    oIAttachmentService.Initialize(AttachDbHelperString.AccountFile);
                    attachList = oIAttachmentService.GetAttachmentModelByObjectID(archiveId);

                    table = PageFileService.GetPageFileByArchiveIdListAndType(new List<string> {archiveId }, archiveTypeId);
                    oIAttachmentService.Initialize(AttachDbHelperString.PageFile);
                    foreach (DataRow accountRow in table.Rows)
                    {
                        if (oIAttachmentService.GetAttachmentModelByObjectID(accountRow["DATAID"].ToString())!=null)
                        {
                            pagefileAttachList.AddRange(
                                oIAttachmentService.GetAttachmentModelByObjectID(accountRow["DATAID"].ToString()));
                        }
                    }
                    
                    break;
                case (int)ArchiveBaseType.AudioVedio:
                    oIAttachmentService.Initialize(AttachDbHelperString.Audiofile);
                    attachList = oIAttachmentService.GetAttachmentModelByObjectID(archiveId);

                    break;
                case (int)ArchiveBaseType.BulidProject:
                    oIAttachmentService.Initialize(AttachDbHelperString.BuildProjectFile);
                    attachList = oIAttachmentService.GetAttachmentModelByObjectID(archiveId);

                    table = PageFileService.GetPageFileByArchiveIdListAndType(new List<string> { archiveId }, archiveTypeId);
                    oIAttachmentService.Initialize(AttachDbHelperString.PageFile);
                    foreach (DataRow buildProjectRow in table.Rows)
                    {
                        if (oIAttachmentService.GetAttachmentModelByObjectID(buildProjectRow["DATAID"].ToString()) != null)
                        {
                            pagefileAttachList.AddRange(
                                oIAttachmentService.GetAttachmentModelByObjectID(buildProjectRow["DATAID"].ToString()));
                        }
                    }
                    break;
                case (int)ArchiveBaseType.CDKind:
                    oIAttachmentService.Initialize(AttachDbHelperString.CDKind);
                    attachList = oIAttachmentService.GetAttachmentModelByObjectID(archiveId);
                    break;
                case (int)ArchiveBaseType.Document:
                case 101:
                case 102:
                    oIAttachmentService.Initialize(AttachDbHelperString.DocumentFile);
                    attachList = oIAttachmentService.GetAttachmentModelByObjectID(archiveId);
                    break;
                case (int)ArchiveBaseType.Equipment:
                    oIAttachmentService.Initialize(AttachDbHelperString.Equipment);
                    attachList = oIAttachmentService.GetAttachmentModelByObjectID(archiveId);

                    table = PageFileService.GetPageFileByArchiveIdListAndType(new List<string> { archiveId }, archiveTypeId);
                    oIAttachmentService.Initialize(AttachDbHelperString.PageFile);
                    foreach (DataRow EquipmentRow in table.Rows)
                    {
                        if(oIAttachmentService.GetAttachmentModelByObjectID(EquipmentRow["DATAID"].ToString())!=null)
                        {
                        pagefileAttachList.AddRange(
                            oIAttachmentService.GetAttachmentModelByObjectID(EquipmentRow["DATAID"].ToString()));
                        }
                    }
                    break;
                case (int)ArchiveBaseType.OliSearch:
                    oIAttachmentService.Initialize(AttachDbHelperString.Oilsearch);
                    attachList = oIAttachmentService.GetAttachmentModelByObjectID(archiveId);
                    break;
                case (int)ArchiveBaseType.RealKind:
                    oIAttachmentService.Initialize(AttachDbHelperString.RealKind);
                    attachList = oIAttachmentService.GetAttachmentModelByObjectID(archiveId);
                    break;
                case (int)ArchiveBaseType.Technology:
                    oIAttachmentService.Initialize(AttachDbHelperString.Technology);
                    attachList = oIAttachmentService.GetAttachmentModelByObjectID(archiveId);

                    table = PageFileService.GetPageFileByArchiveIdListAndType(new List<string> { archiveId }, archiveTypeId);
                    oIAttachmentService.Initialize(AttachDbHelperString.PageFile);
                    foreach (DataRow techRow in table.Rows)
                    {
                        if (oIAttachmentService.GetAttachmentModelByObjectID(techRow["DATAID"].ToString()) != null)
                        {
                            pagefileAttachList.AddRange(
                                oIAttachmentService.GetAttachmentModelByObjectID(techRow["DATAID"].ToString()));
                        }
                    }
                    break;
            }
            if (attachList != null)
            {
                foreach (var attach in attachList)
                {
                    attachIdList.Add(attach.AttachmentID);
                }
            }
            if (pagefileAttachList != null)
            {
                foreach (var attach in pagefileAttachList)
                {
                    if (!attachIdList.Contains(attach.AttachmentID))
                    {
                        attachIdList.Add(attach.AttachmentID);
                    }
                }
            }
            
            techFileCount = attachIdList.Count.ToString();
            
            return techFileCount.Equals("0") ? string.Empty : techFileCount;
        }

        /// <summary>
        /// 备注：根据档案分类和档案ID取电子文件数目
        /// 时间：2013年5月23日 13:24:17
        /// </summary>
        /// <param name="archiveTypeId"></param>
        /// <param name="archiveId"></param>
        /// <returns></returns>
        public string GetPageFileTechnologyFileCount(int archiveTypeId, string archiveId)
        {
            string techFileCount = string.Empty;
            IAttachmentService oIAttachmentService = UContainer.Resolve<IAttachmentService>();
            IList<AttachmentModel> pagefileAttachList = new List<AttachmentModel>();
            List<string> attachIdList = new List<string>();
            oIAttachmentService.Initialize(AttachDbHelperString.PageFile);
            pagefileAttachList = oIAttachmentService.GetAttachmentModelByObjectID(archiveId);
            if (pagefileAttachList != null)
            {
                foreach (var attach in pagefileAttachList)
                {
                    attachIdList.Add(attach.AttachmentID);
                }
            }
            techFileCount = attachIdList.Count.ToString();
            return techFileCount.Equals("0") ? string.Empty : techFileCount;
        }

        /// <summary>
        /// 根据卷DataId取卷内件
        /// 时间：2014年6月22日 13:04:51
        /// </summary>
        /// <param name="archiveId"></param>
        /// <returns></returns>
        public DataTable GetPageFileByDataId(string dataId, int archiveTypeId)
        {
            DataTable archiveTable = null;

            switch (archiveTypeId)
            {
                //会计类
                case ((int)ArchiveBaseType.Account):
                    IAccountArchiveService accountArchiveService = UContainer.Resolve<IAccountArchiveService>();
                    archiveTable = accountArchiveService.GetPageFileByDATAID(dataId);
                    break;
                //基建类
                case ((int)ArchiveBaseType.BulidProject):
                    IBuildProjectArchiveService buildProjectService = UContainer.Resolve<IBuildProjectArchiveService>();
                    archiveTable = buildProjectService.GetPageFileByDATAID(dataId);
                    break;
                //设备类
                case ((int)ArchiveBaseType.Equipment):
                    IEquipmentService equipService = UContainer.Resolve<IEquipmentService>();
                    archiveTable = equipService.GetPageFileByDATAID(dataId);
                    break;
                //油气勘探类
                case ((int)ArchiveBaseType.OliSearch):
                    IOilSearchArchiveService oliSearchService = UContainer.Resolve<IOilSearchArchiveService>();
                    archiveTable = oliSearchService.GetPageFileByDATAID(dataId);
                    break;
                //科研类
                case ((int)ArchiveBaseType.Technology):
                    ITechArchiveService techService = UContainer.Resolve<ITechArchiveService>();
                    archiveTable = techService.GetPageFileByDATAID(dataId);
                    break;
            }
            return archiveTable;
        }
    }
}